package dailytest;

import org.apache.commons.collections4.CollectionUtils;

import java.util.*;
import java.util.concurrent.*;
import java.util.stream.Collectors;


/**
 * @Author: youdao.hww
 * @Date: 3/19/24
 * @Desc:
 */
public class ThreadPageTest {
    private static Map<Integer, List<String>> pageMap = new HashMap<>();

    static {
        pageMap.put(1, Arrays.asList("1", "2", "3"));
        pageMap.put(2, Arrays.asList("a", "b", "c"));
        pageMap.put(3, Arrays.asList("d", "e", "f"));
        pageMap.put(4, Arrays.asList("d1", "e1", "f1"));
        pageMap.put(5, Arrays.asList("d2", "e2", "f2"));
        pageMap.put(6, Arrays.asList("d3", "e3", "f3"));
        pageMap.put(7, Arrays.asList("d4", "e4", "f4"));
        pageMap.put(8, Arrays.asList("v5", "s5"));
    }


    public static void main(String[] args) {
        //最大上传数量
        int pageSize = 3;
        // 创建一个固定大小的线程池
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        //存放订单id的集合
        List<Future<List<String>>> futures = new ArrayList<>();
        //默认第一页
        int currentPage = 1;
        try {
            while (true) {
                // 提交任务到线程池
                int finalCurrentPage = currentPage;
                currentPage++;
                Future<List<String>> future = executorService.submit(() -> {
                    //一页的费用
                    ThreadPage threadPage = ThreadPage.builder().page(finalCurrentPage).build();
                    List<String> pageTransferFees = pageMap.get(threadPage.getPage());
                    if (CollectionUtils.isNotEmpty(pageTransferFees)) {
                        System.out.println("第" + finalCurrentPage + "，pageTransferFees = " + pageTransferFees);
                        return pageTransferFees;
                    }
                    return new ArrayList<>();
                });
                futures.add(future);
                // 当前页数据不满一页数，表示没有更多数据了，退出循环
                if (future.get().size() < pageSize) {
                    System.out.println("future = " + future.get().size());
                    System.out.println("pageSize = " + pageSize);
                    break;
                }
            }
            //合并临时单
            List<String> transferFees = futures.stream().flatMap(item -> {
                try {
                    return item.get().stream();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }).collect(Collectors.toList());

            System.out.println("transferFees = " + transferFees);

        } catch (Exception e) {

        } finally {
            executorService.shutdown();
        }
    }


    private static List<String> pageInpatientTransferFees(ThreadPage threadPage) {


        return null;
    }


}
